<?php
namespace app\system\controller;

use app\system\model\GroupLinkModel;
use app\system\model\LinkModel;

class Link extends Common
{
    protected $pageSize = 20;

    /**
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index()
    {

        $linkModel = new LinkModel();
        $list = $linkModel->selectLink(['is_delete'=>0]);
        $list = classsorting(obj2array($list));

        $assign = [
            'list' => $list,
        ];
        return $this->fetch('', $assign);
    }

    public function savesort()
    {
        !isAjaxPost() && die;
        $post = input('post.');
        $linkModel = new LinkModel();
        foreach ($post as $k=>$v) {
            if ($k > 0) {
                $linkModel->updateLink(['sort'=>$v], ['id'=>$k]);
            }
        }
        return $this->jsonSuccess('修改成功');
    }

    public function del()
    {
        !isAjaxPost() && die();
        $id = input('post.id', 0, 'intval');
        $linkModel = new LinkModel();
        $groupLinkModel = new GroupLinkModel();
//        if ($groupLinkModel->countGroupLink(['link_id'=>$id, 'group_id'=>1]) > 0) {
//            return $this->jsonError('已有用户组拥有该权限，请先修改用户组权限');
//        }

        if ($linkModel->countLink(['pid' => $id, 'is_delete' => 0]) > 0) {
            return $this->jsonError('该链接拥有子链接，请先删除子链接');
        }

        if ($linkModel->deleteLink(['id' => $id])) {
            return $this->jsonSuccess('删除成功');
        } else {
            return $this->jsonError('删除失败');
        }
    }

    /**
     * @return mixed|\think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function add()
    {
        if (isAjaxPost()) {
            $data = input('post.');

            //表单自动验证
            $validate = new \app\system\validate\Link();
            if (!$validate->check($data)) {
                return $this->jsonError($validate->getError());
            }

            $linkModel = new LinkModel();
            $data['time'] = time();
            if ($lid = $linkModel->addLink($data)) {
                //超级管理员添加该权限
                $groupLinkModel = new GroupLinkModel();
                $groupLinkModel->addGroupLink(['group_id'=>1,'link_id'=>$lid]);

                return $this->jsonSuccess('添加成功');
            } else {
                return $this->jsonError('添加失败');
            }
        }

        $linkModel = new LinkModel();
        $list = $linkModel->selectLink(['is_delete'=>0]);
        $list = classsorting(obj2array($list));

        $assign = [
            'list' => $list,
        ];
        return $this->fetch('', $assign);
    }

    /**
     * @param $id
     * @return mixed|\think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function save($id)
    {
        $id = intval($id);
        if ($id <= 0) {
            $this->redirect('index');
        }

        $linkModel = new LinkModel();

        if (isAjaxPost()) {
            $data = input('post.');

            //表单自动验证
            $validate = new \app\system\validate\Link();
            if (!$validate->check($data)) {
                return $this->jsonError($validate->getError());
            }

            if ($linkModel->updateLink($data, ['id'=>$id]) !== false) {
                return $this->jsonSuccess('修改成功');
            } else {
                return $this->jsonError('修改失败');
            }
        }

        $data = $linkModel->findLink(['id'=>$id]);
        $list = $linkModel->selectLink(['is_delete'=>0]);
        //当前级的下级不能选中
        $nextLink = classification(obj2array($list), $id);
        $nextLink[] = $id; //自己也不能选中
        if(! empty($nextLink) ){
            foreach ($list as $k=>$v){
                if(in_array($v['id'], $nextLink)){
                    $list[$k]['disabled'] = true;
                }
            }
        }
        $list = classsorting($list);


        $assign = [
            'data' => $data,
            'list' => $list
        ];
        return $this->fetch('', $assign);
    }
}